package com.example.statemachine.core;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.function.Supplier;

@Component
public class StateMachineTemplate {

    private final StateMachineEngine engine;

    @Autowired
    public StateMachineTemplate(StateMachineEngine engine) {
        this.engine = engine;
    }

    public <T> TransitionResult<T> sendEvent(TransitionRequest<T> request, Class<T> entityClass) {
        return engine.executeTransition(request, entityClass);
    }

    public <T> TransitionResult<T> sendEvent(String businessId, StateEnum currentState,
                                             EventEnum event, String operator,
                                             Supplier<T> entityLoader, Class<T> entityClass) {
        // 修正：在创建Context时传入event
        StateMachineContext context = new StateMachineContext(businessId, currentState, event, operator);
        TransitionRequest<T> request = new TransitionRequest<>(currentState, event, context, entityLoader);

        return sendEvent(request, entityClass);
    }

    public <T> TransitionResult<T> sendEvent(String businessId, StateEnum currentState,
                                             EventEnum event, String operator, Class<T> entityClass) {
        throw new StateMachineException("请使用包含entityLoader参数的方法");
    }
}